<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends BaseController {
    //首页
    public function index(){
        $this->display('Index/index');
    }
    //代理列表
    public function adminList(){
        $keywords=I('get.keywords');
        $keyvalue=trim(I('get.keyvalue'));
        $where='where a.role=1';
        if ($keywords!='' && $keyvalue!='') {
            $where.=' and a.'.$keywords.' like "%'.$keyvalue.'%"';
        }else if ($keywords!='' && $keyvalue=='') {
            $where.='';
        }else if ($keywords=='' && $keyvalue!='') {
            $where.=' and a.admin_id like "%'.$keyvalue.'%" or a.truename like "%'.$keyvalue.'%" or a.phone like "%'.$keyvalue.'%" or a.id_no like "%'.$keyvalue.'%" or a.pid like "%'.$keyvalue.'%"';
        }
        $re=M('')->query('select a.*,FROM_UNIXTIME(a.addtime, "%Y-%m-%d %H:%i:%S") as addtime,(select count(*) from gm_admin b where a.admin_id = b.pid) sum from gm_admin a '.$where);
        if (!$re) {
            $re='';
        }
        $data=$this->showPage($re);
        
        $this->assign('adminList',$data['arr']);
        $this->assign('page',$data['pageStr']);
        $this->assign('keywords',$keywords);
        $this->assign('keyvalue',$keyvalue);
        $this->display();
    }

    //购买记录
    public function buyList(){
        $keywords=I('get.keywords');
        $keyvalue=trim(I('get.keyvalue'));
        if ($keywords!='' && $keyvalue!='') {
            if ($keywords=='truename') {
               $where='where a.'.$keywords.' like "%'.$keyvalue.'%" ';
            }else{
                $where='where b.'.$keywords.' like "%'.$keyvalue.'%" ';
            }
        }else if ($keywords!='' && $keyvalue=='') {
            $where='';
        }else if ($keywords=='' && $keyvalue!='') {
            $where='where b.status like "%'.$keyvalue.'%" or a.truename like "%'.$keyvalue.'%" ';
        }
        $re=M('')->query('select b.*,FROM_UNIXTIME(b.addtime, "%Y-%m-%d %H:%i:%S") as addtime,FROM_UNIXTIME(b.modify_time, "%Y-%m-%d %H:%i:%S") as modify_time,a.truename,a.phone from gm_buy b join gm_admin a on a.admin_id=b.admin_id '.$where.'order by b.status asc');
        // var_dump($re);die;
        if (!$re) {
            $re='';
        }
        $data=$this->showPage($re);
        
        $this->assign('adminList',$data['arr']);
        $this->assign('page',$data['pageStr']);
        $this->assign('keywords',$keywords);
        $this->assign('keyvalue',$keyvalue);
        $this->display();
    }
    //购买申请
    public function buyApply(){
        $this->display();
    }
    //代理购买申请数据保存
    public function buySave(){
        $data=I('post.');
        $data['admin_id']=$this->admin_id;
        $data['addtime']=time();
        $re=M('Buy')->add($data);
        if ($re) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('false');
        }
    }
    //返利记录
    public function rebateList(){
        $keywords=I('get.keywords');
        $keyvalue=trim(I('get.keyvalue'));
        if ($keywords!='' && $keyvalue!='') {
            if ($keywords=='truename') {
               $where='where a.'.$keywords.' like "%'.$keyvalue.'%" ';
            }else{
                $where='where r.'.$keywords.' like "%'.$keyvalue.'%" ';
            }
        }else if ($keywords!='' && $keyvalue=='') {
            $where='';
        }else if ($keywords=='' && $keyvalue!='') {
            $where='where r.status like "%'.$keyvalue.'%" or a.truename like "%'.$keyvalue.'%" ';
        }
        $re=M('')->query('select r.*,FROM_UNIXTIME(r.addtime, "%Y-%m-%d %H:%i:%S") as addtime,a.truename,a.phone from gm_rebate r join gm_admin a on a.admin_id=r.admin_id '.$where.'order by r.id desc');
        // var_dump($re);die;
        if (!$re) {
            $re='';
        }
        $data=$this->showPage($re);
        
        $this->assign('adminList',$data['arr']);
        $this->assign('page',$data['pageStr']);
        $this->assign('keywords',$keywords);
        $this->assign('keyvalue',$keyvalue);
        $this->display();
    }
    //提现记录
    public  function withdrawList(){
        $keywords=I('get.keywords');
        $keyvalue=trim(I('get.keyvalue'));
        if ($keywords!='' && $keyvalue!='') {
            if ($keywords=='truename') {
               $where='where a.'.$keywords.' like "%'.$keyvalue.'%" ';
            }else{
                $where='where r.'.$keywords.' like "%'.$keyvalue.'%" ';
            }
        }else if ($keywords!='' && $keyvalue=='') {
            $where='';
        }else if ($keywords=='' && $keyvalue!='') {
            $where='where r.status like "%'.$keyvalue.'%" or a.truename like "%'.$keyvalue.'%" ';
        }
        $re=M('')->query('select r.*,FROM_UNIXTIME(r.addtime, "%Y-%m-%d %H:%i:%S") as addtime,a.truename,a.phone from gm_withdraw r join gm_admin a on a.admin_id=r.admin_id '.$where.'order by r.id desc');
        // var_dump($re);die;
        if (!$re) {
            $re='';
        }
        $data=$this->showPage($re);
        
        $this->assign('adminList',$data['arr']);
        $this->assign('page',$data['pageStr']);
        $this->assign('keywords',$keywords);
        $this->assign('keyvalue',$keyvalue);
        $this->display();
    }
    //提现申请
    public function withdrawApply(){
        $adminOb=M('Admin');
        $admin_id=$this->admin_id;
        $re=$adminOb->field('total_rebate,total_withdraw')->where("admin_id=".$admin_id)->find();
        $w=M('Withdraw')->where("admin_id=".$admin_id.' and status=0')->sum('amount');
        $rebate=$re['total_rebate']-$re['total_withdraw']-$w;
        $this->assign('rebate',$rebate);
        $this->display();
    }
    //提现申请数据保存
    public function withdrawSave(){
        $data=I('post.');
        $data['admin_id']=$this->admin_id;
        $data['addtime']=time();
        $re=M('Withdraw')->add($data);
        if ($re) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('false');
        }
    }
    //给玩家充值
    public function pay(){
        $adminOb=M('Admin');
        $admin_id=$this->admin_id;
        $re=$adminOb->field('(total_pay-total_consume) remain')->where("admin_id=".$admin_id)->find();
        $this->assign('remain',$re['remain']);
        $this->display();
    }
    //玩家充值数据保存,调游戏接口
    public function paySave(){
        $data=I('post.');
       // $is_gamer=M('Gamer')->field('gamer_id')->where('openid='.$data['openid'])->find();  //马锐修改
        $is_gamer=M('Gamer')->where('openid='.$data['openid'])->find();     //查询赋值整条数据 同上面相同    
        if (!$is_gamer['gamer_id']) {
            $this->ajaxReturn('nexist');
        }
        $data['nickname'] = $is_gamer['nickname'];   //马锐修改  赋值玩家微信名
        $data['admin_id']=$this->admin_id;
        $data['addtime']=time();
        $re=M('Pay')->add($data);
        if ($re) {
            // $url=C('API_URL').'/?mod=gm&act=addPlayerCard&uid='.$data['openid'].'&args='.$data['num'];
            $url = C('API_URL') . '/?mod=gm&act=modifyCard&uid=' . $data['openid'] . '&card=' . $data['num'];
            $curl_res=get_curl($url);
            if (!$curl_res['code']==0){
                $id=M('Pay')->getLastInsID();
                M('Pay')->where('id='.$id)->delete();
                $this->ajaxReturn('false');
            }else{
                //玩家总充值数累加
                $r=M('Gamer')->where('openid='.$data['openid'])->setInc('total_pay',$data['num']);
                if ($r) {
                    //代理总消费数累加
                    M('Admin')->where('admin_id='.$data['admin_id'])->setInc('total_consume',$data['num']);
                }
                $data['status'] = success ;     //马锐修改  充值成功状态
                $this->ajaxReturn($data);   //马锐修改  回调数组  
                //$this->ajaxReturn('success');
            }
        }else{
            $this->ajaxReturn('false');
        }
    }
    //账号封停
    public function blacklist(){
        $admin_id=I('get.admin_id');
        $status=I('get.status');
        $re=M('Admin')->where('admin_id='.$admin_id)->save(array('status'=>$status));
        if ($re) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('false');
        }
    }
    //修改密码
    public function modifyPwd(){
        $this->display();
    }
    //密码保存
    public function pwdSave(){
        $old_password=I('post.old_password');
        $password=I('post.password');
        $admin_id=$this->admin_id;
        $re=M('Admin')->field('password')->where('admin_id='.$admin_id)->find();
        if (md5($old_password)==$re['password']) {
            $r=M('Admin')->where('admin_id='.$admin_id)->save(array('password'=>md5($password)));
            if ($r) {
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('false');
            }
        }else{
            $this->ajaxReturn('error');
        }
    }
}